package com.cuihaodong.client;

import com.cuihaodong.pojo.InstanceInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.Map;

/**
 * @author cuihaodong
 */
@Component
public class EurekaClientConsumer {

    @Autowired
    private RestTemplate restTemplate;

    @Value("${eurekaServer.ip}")
    private String serverIp;

    @Value("${eurekaServer.port}")
    private String serverPort;

    /**
     * 每隔60s拉取一次服务
     */
    @Scheduled(cron = "*/3 * * * * ?")
    public void pullServices(){

        Map<String,InstanceInfo> map = new HashMap<String, InstanceInfo>();

        if(map.size()>0){
            //清除原有的服务
            map.clear();
        }
        String url = "http://"+serverIp+":"+serverPort+"/eurekaServer/pullServices";
        Map<String,InstanceInfo> result = restTemplate.getForObject(url,Map.class);
        System.out.println(result);
        //将服务存储到map集合中
        map.putAll(result);
    }
}
